<?php # ProcessGuestRegistration.php
require "customers.class.php";
require "address.class.php";

header("Content-Type: text/xml");
echo '<?xml version="1.0" encoding="utf-8"
standalone="yes" ?>
<response>';

/*
 * Specify the field names that are in the form.
 */
$allowedFields = array(
 			'EmailAddress',
			'Password',
			'PasswordConfirm',
			'Title',
			'FirstName',
			'LastName',
			'DOB',
			'HomePhone',
			'MobilePhone',
			'StreetAddress',
			'City',
			'StateOrProvince',
			'ZIP'
);

/*
 * Specify the field names that are required...
 */
$requiredFields = array(
			'EmailAddress',
			'Password',
			'PasswordConfirm',
			'FirstName',
			'LastName',
			'DOB',
			'HomePhone',
			'StreetAddress',
			'City',
			'StateOrProvince',
			'ZIP'
);

$error = false;
foreach($_POST AS $key => $value)
{
	// first make sure this is an allowed field
	if(in_array($key, $allowedFields))
	{
		$$key = $value;
		
		// is this a required field?
		if(in_array($key, $requiredFields) && $value == '')
		{
			$error = true;
			echo '<error>The field '.$key.' is required.</error>';
		}
	}
	else
	{
		$error = true;
		echo '<error>The field '.$key.' does not exist.</error>';
	}
}



if (!$error) {
	$emailAddress = $_POST['EmailAddress'];
	if (isset($_POST['EmailAddress'])){
		$emailAddress = $_POST['EmailAddress'];
	}
	// check to make sure that customer with the same email doesn't exist.
	$customer = Customers::loadByCustomerEmail($emailAddress);
  	if($customer->doesExist() == 0) {
		// create a new customer and initialize the customer with the POST variables.
		$customer = new Customers;
		$customer->initPOST();
		
		// create a new address and initialize the address with the POST variables.
		$address = new Address;
		$address->initPOST();
		
		// check to make sure the data is the correct length before inserting into database.
		if(strlen($customer->getCustomerTitle()) > 10) {
			$error = true;
			echo '<error>Title does not allow strings longer than 10 characters.</error>';
		}
		if(strlen($customer->getCustomerLastName()) > 45) {
			$error = true;
			echo '<error>Last Name does not allow strings longer than 45 characters.</error>';
		}
		if(strlen($customer->getCustomerFirstName()) > 45) {
			$error = true;
			echo '<error>First Name does not allow strings longer than 45 characters.</error>';
		}
		if(strlen($customer->getCustomerHomePhone()) > 20) {
			$error = true;
			echo '<error>Home Phone does not allow strings longer than 20 characters.</error>';
		}
		if(strlen($customer->getCustomerMobilePhone()) > 20) {
			$error = true;
			echo '<error>Mobile Phone does not allow strings longer than 20 characters.</error>';
		}
		if(strlen($customer->getCustomerEmail()) > 45) {
			$error = true;
			echo '<error>Email Address does not allow strings longer than 45 characters.</error>';
		}
		if(!filter_var($_POST['EmailAddress'], FILTER_VALIDATE_EMAIL)) {
			$error = true;
			echo '<error>Please provide a valid email address.</error>';
		}
		if(strlen($customer->getPassword()) > 45) {
			$error = true;
			echo '<error>Password does not allow strings longer than 45 characters.</error>';
		}
		if($_POST['Password'] != $_POST['PasswordConfirm']) {
			$error = true;
			echo '<error>The 2 passwords you have entered do not match.</error>';
		}
		if(strlen($address->getStreet()) > 100) {
			$error = true;
			echo '<error>Street Address does not allow strings longer than 100 characters.</error>';
		}
		if(strlen($address->getCity()) > 45) {
			$error = true;
			echo '<error>City does not allow strings longer than 45 characters.</error>';
		}
		if(strlen($address->getStateOrProvince()) > 45) {
			$error = true;
			echo '<error>StateOrProvince does not allow strings longer than 45 characters.</error>';
		}
		if(strlen($address->getPostalCode()) > 20) {
			$error = true;
			echo '<error>ZIP Code does not allow strings longer than 20 characters.</error>';
		}
		
		if(!$error) {
			$addressId = $address->insert();
			$address->setAddressId($addressId);
			$customer->setAddressId($addressId);
			$customerId = $customer->insert();
			$customer->setCustomerId($customerId);
			echo '<result>Success</result>';
		}
	}
	else {
		$error = true;
		echo '<error>Guest with email address '.$emailAddress.' already exists.</error>';
	}
}

echo '</response>';
?>
